$(document).ready(function () {


    $('.lb-gallery-item-d').hover(
        function () {
            $(this).find('.grid-grayscale').fadeOut('fast');
            $(this).find('.grid-overlay').animate({bottom: '0px'}, 200);
            $(this).find('img').animate({top: '-56px'}, 200);
        },
        function () {
            $(this).find('.grid-grayscale').fadeIn('fast');
            $(this).find('img').animate({top: '0px'}, 200);
            $(this).find('.grid-overlay').animate({bottom: '-56px'}, 200);
        }
    );

    $('.small-trigger').click(function () {
        var $new_slider = $(this).closest('.blog-row-item').find('ul').html();
        var $slider_content = $('.artwork-zoom-img ul.slides');
        var $title = $(this).closest('.blog-row-item').find('.sm-gallery-lbl').text();
        var $category = $(this).closest('.blog-row-item').find('.sm-gallery-cateory').html();
        var $text = $(this).closest('.blog-row-item').find('.sm-gallery-txt').html();
        $('.st-gallery-lbl').text($title);
        $('.st-gallery-cat').html($category);
        $('.st-gallery-content').html($text);
        $slider_content.html($new_slider);
        $('.blog-row-item').removeClass('current');
        $(this).closest('.blog-row-item').addClass('current');
        $('.flexslider').removeData("flexslider").flexslider();

        $('.lb-overlay').fadeIn(function () {
            $('.artwork-zoom').animate({top: "50%"}, 350, function () {
            });
        });
        var $slides_total = $('.artwork-zoom-img .slides li').length;
        $('.artwork-zoom-img .flex-direction-nav li').eq('0').append('<span class="items-count-prev"><span>1</span> of ' + $slides_total + '</span>');
        $('.artwork-zoom-img .flex-direction-nav li').eq('1').append('<span class="items-count-next"><span>1</span> of ' + $slides_total + '</span>');
        return false;

    });

    $('.lb-zoom-btn').click(function () {
        var $new_slider = $(this).closest('.blog-row-item').find('ul').html();
        var $slider_content = $('.artwork-zoom-img ul.slides');
        $slider_content.html($new_slider);
        $('.blog-row-item').removeClass('current');
        $(this).closest('.blog-row-item').addClass('current');
        $('.flexslider').removeData("flexslider").flexslider();
        $('.lb-overlay').fadeIn(function () {
            $('.artwork-zoom').animate({top: "50%"}, 350, function () {
            });
        });

        var $slides_total = $('.artwork-zoom-img .slides li').length;
        $('.artwork-zoom-img .flex-direction-nav li').eq('0').append('<span class="items-count-prev"><span>1</span> of ' + $slides_total + '</span>');
        $('.artwork-zoom-img .flex-direction-nav li').eq('1').append('<span class="items-count-next"><span>1</span> of ' + $slides_total + '</span>');
        return false;
    });


    $('.lb-zoom-prev').click(function () {
        var $next = $('.blog-row-item.current').prev('.blog-row-item');
        var $new_slider = $next.find('ul').html();
        var $slider_content = $('.artwork-zoom-img ul.slides');
        var $index = $('.blog-row-item.current').index();
        var $title = $next.find('.sm-gallery-lbl').text();
        var $category = $next.find('.sm-gallery-cateory').html();
        var $text = $next.find('.sm-gallery-txt').html();
        if ($index == '0') {
            return false;
        } else {
            $('.st-gallery-lbl').text($title);
            $('.st-gallery-cat').html($category);
            $('.st-gallery-content').html($text);
            $slider_content.html($new_slider);
            $('.flex-direction-nav').remove();
            $('.flex-control-nav').remove();
            $('.flexslider').removeData("flexslider").flexslider();
            $('.blog-row-item').removeClass('current');
            $next.addClass('current');
            var $slides_total = $('.artwork-zoom-img .slides li').length;
            $('.artwork-zoom-img .flex-direction-nav li').eq('0').append('<span class="items-count-prev"><span>1</span> of ' + $slides_total + '</span>');
            $('.artwork-zoom-img .flex-direction-nav li').eq('1').append('<span class="items-count-next"><span>1</span> of ' + $slides_total + '</span>');

            return false;
        }
    });
    $('.lb-zoom-next').click(function () {
        var $next = $('.blog-row-item.current').next('.blog-row-item');
        var $new_slider = $next.find('ul').html();
        var $slider_content = $('.artwork-zoom-img ul.slides');
        var $title = $next.find('.sm-gallery-lbl').text();
        var $category = $next.find('.sm-gallery-cateory').html();
        var $text = $next.find('.sm-gallery-txt').html();
        var $index = $('.blog-row-item.current').index();
        var $length = $('.blog-row-item').length;
        if ($index == $length - 1) {
            return false;
        } else {
            $('.st-gallery-lbl').text($title);
            $('.st-gallery-cat').html($category);
            $('.st-gallery-content').html($text);
            $slider_content.html($new_slider);
            $('.flex-direction-nav').remove();
            $('.flex-control-nav').remove();
            $('.flexslider').removeData("flexslider").flexslider({animation: "fade", slideshow: false});
            $('.blog-row-item').removeClass('current');
            $next.addClass('current');
            var $slides_total = $('.artwork-zoom-img .slides li').length;
            $('.artwork-zoom-img .flex-direction-nav li').eq('0').append('<span class="items-count-prev"><span>1</span> of ' + $slides_total + '</span>');
            $('.artwork-zoom-img .flex-direction-nav li').eq('1').append('<span class="items-count-next"><span>1</span> of ' + $slides_total + '</span>');

            return false;
        }
    });

    $('.st-gallery-item a').click(function () {
        var $width = $('.st-gallery-right').width();
        var $slider_content = $('.st-gallery-right-b ul.slides');
        var $new_slider = $(this).closest('.st-gallery-item').find('.new-slides').html();
        var $title = $(this).closest('.st-gallery-item').find('.artwork-foot-lbl').text();
        var $category = $(this).closest('.st-gallery-item').find('.artwork-foot-cat').text();
        var $category_link = $(this).closest('.st-gallery-item').find('.artwork-foot-cat').data('link');
        var $text = $(this).closest('.st-gallery-item').find('.st-gallery-item-text').html();
        $('.st-gallery-item').removeClass('current');
        $(this).closest('.st-gallery-item').addClass('current');
        $('#primary-container').css('margin-top', '-20000px');
        $('.st-gallery-wrapper').fadeIn();
        $('.st-gallery-wrapper li').width($width);
        $slider_content.html($new_slider);
        $('.flexslider').removeData("flexslider").flexslider();
        $('.st-gallery-cat a').text($category);
        $('.st-gallery-lbl').text($title);
        $('.st-gallery-content').html($text);
        $('st-gallery-cat a').attr('href', $category_link);
        var $slides_total = $('.st-gallery-right-b .slides li').length;
        $('.st-gallery-right-b .flex-direction-nav li').eq('0').append('<span class="items-count-prev"><span>1</span> of ' + $slides_total + '</span>');
        $('.st-gallery-right-b .flex-direction-nav li').eq('1').append('<span class="items-count-next"><span>1</span> of ' + $slides_total + '</span>');

        return false;
    });

    $('.st-gallery-right-b .flex-direction-nav li').live('mouseover mouseout', function (event) {
        if (event.type == 'mouseover') {

            $(this).find('span').fadeIn();
        } else {
            $(this).find('span').hide();
        }
    });

    $('.st-gallery-right-b .flex-prev,.st-gallery-right-b .flex-next').live('click', function () {
        var $index = $('.st-gallery-right-b .slides li.flex-active-slide').index();
        $('.items-count-prev span').text($index + 1);
        $('.items-count-next span').text($index + 1);
    });


    $('.st-gallery-all.standart').click(function () {
        $('.st-gallery-wrapper').hide();
        $('#primary-container').css('margin-top', '0px');
        $('.flexslider').removeData("flexslider");
        $('.flex-direction-nav').remove();
        $('.flex-control-nav').remove();
        return false;
    });

    $('.st-gallery-next').click(function () {
        var $current = $('.st-gallery-item.current');
        var $next = $current.next('.st-gallery-item');
        var $slider_content = $('.st-gallery-right-b ul.slides');
        var $new_slider = $next.find('.new-slides').html();
        var $title = $next.find('.artwork-foot-lbl').text();
        var $category = $next.find('.artwork-foot-cat').text();
        var $category_link = $next.find('.artwork-foot-cat').data('link');
        var $text = $next.find('.st-gallery-item-text').html();
        var $index = $current.index();
        var $lenght = $('.st-gallery-item').length;
        if ($index == $lenght - 1) {
            return false;
        } else {
            $('.st-gallery-item').removeClass('current');
            $next.addClass('current');
            $slider_content.html($new_slider);
            $('.flex-direction-nav').remove();
            $('.flex-control-nav').remove();
            $('.flexslider').removeData("flexslider").flexslider();
            $('.st-gallery-cat a').text($category);
            $('.st-gallery-lbl').text($title);
            $('.st-gallery-content').html($text);
            $('st-gallery-cat a').attr('href', $category_link);
            var $slides_total = $('.st-gallery-right-b .slides li').length;
            $('.st-gallery-right-b .flex-direction-nav li').eq('0').append('<span class="items-count-prev"><span>1</span> of ' + $slides_total + '</span>');
            $('.st-gallery-right-b .flex-direction-nav li').eq('1').append('<span class="items-count-next"><span>1</span> of ' + $slides_total + '</span>');
        }

        return false;
    });
    $('.st-gallery-prev').click(function () {
        var $current = $('.st-gallery-item.current');
        var $next = $current.prev('.st-gallery-item');
        var $slider_content = $('.st-gallery-right-b ul.slides');
        var $new_slider = $next.find('.new-slides').html();
        var $title = $next.find('.artwork-foot-lbl').text();
        var $category = $next.find('.artwork-foot-cat').text();
        var $category_link = $next.find('.artwork-foot-cat').data('link');
        var $text = $next.find('.st-gallery-item-text').html();
        var $index = $current.index();
        if ($index == '0') {
            return false;
        } else {
            $('.st-gallery-item').removeClass('current');
            $next.addClass('current');
            $slider_content.html($new_slider);
            $('.flex-direction-nav').remove();
            $('.flex-control-nav').remove();
            $('.flexslider').removeData("flexslider").flexslider();
            $('.st-gallery-cat a').text($category);
            $('.st-gallery-lbl').text($title);
            $('.st-gallery-content').html($text);
            $('st-gallery-cat a').attr('href', $category_link);
            var $slides_total = $('.st-gallery-right-b .slides li').length;
            $('.st-gallery-right-b .flex-direction-nav li').eq('0').append('<span class="items-count-prev"><span>1</span> of ' + $slides_total + '</span>');
            $('.st-gallery-right-b .flex-direction-nav li').eq('1').append('<span class="items-count-next"><span>1</span> of ' + $slides_total + '</span>');
        }

        return false;
    });

});
